import Compressor from 'compressorjs'
export function dataURLtoBlob(dataurl:string) {
    var arr = dataurl.split(',')//分割为数组，分割到第一个逗号
    let mime = (arr[0] as any).match(/:(.*?);/)[1]//获取分割后的base64前缀中的类型
    let bstr = window.atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
}

export const beforeRead = (file: File) => {
    return new Promise((resolve) => {
      // compressorjs 默认开启 checkOrientation 选项
      // 会将图片修正为正确方向
      new Compressor(file, {
        success(res) {
          console.log(res, 'ggggg')
          let img = new FileReader()
          img.readAsDataURL(res)
          console.log('img:', img)
          img.onload = ({ target }) => {
            resolve((target as FileReader).result)
            console.log('target:', target, '移动端修正后的图片数据')
          }
        },
        error(err) {
          console.log(err.message)
        },
      })
    })
  }